package com.dd.demo.day1;

import java.util.*;

public class TestObj {
	public TestObj() {
	}

	public TestObj(Long id, String name) {
		this.id = id;
		this.name = name;
	}

	private Long id;
	private String name;

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}


	public static void main(String[] args) {
		List<TestObj> testObjs = test1();//1,把对象或属性分别存入 List<TestObj> List<Long> List<String>
		Set<TestObj> testObjs1 = test2();//2,把对象或属性分别存入 Set<TestObj>
		test3(testObjs);//3, List<TestObj> 排序, 默认排序 和 自定义排序
		Map<Long, TestObj> map = test4(testObjs);//4,循环 List<TestObj> 将数据转化为 Map<Long,TestObj>;
		Map<Long, String> longStringMap = test5(testObjs); //5,  循环 List<TestObj> 将数据转化为 Map<Long,String>;
		test6(testObjs1);//6,循环 Set<TestObj> 将数据转化为 Map<Long,TestObj>;
		test7(testObjs1);//7,循环 Set<TestObj> 将数据转化为 Map<Long,String>;
		test8(map);//8,循环 Map<Long,TestObj>, 获得 List<TestObj>
		test9(longStringMap);//9,循环 Map<Long,String>, 获得 List<TestObj>

	}

	private static List<TestObj> test1() {
		List<TestObj> list = new ArrayList<>();
		for (int i = 0; i < 3; i++) {
			TestObj testObj = new TestObj();
			testObj.setId(Long.valueOf(i));
			testObj.setName("name:" + i);
			list.add(testObj);
		}
		return list;
	}

	private static Set<TestObj> test2() {
		Set<TestObj> list = new HashSet<TestObj>();
		for (int i = 0; i < 3; i++) {
			TestObj testObj = new TestObj();
			testObj.setId(Long.valueOf(i));
			testObj.setName("name:" + i);
			list.add(testObj);
		}
		return list;
	}

	private static void test3(List<TestObj> testObjs) {
		Collections.sort(testObjs, new Comparator<TestObj>() {
			@Override
            public int compare(TestObj s1, TestObj s2) {
				return s1.getId().compareTo(s2.getId());
			}
		});
	}

	private static Map<Long, TestObj> test4(List<TestObj> testObjs) {
		Map<Long, TestObj> map = new HashMap<Long, TestObj>();
		for (TestObj testObj : testObjs) {
			map.put(testObj.getId(), testObj);
		}
		return map;
	}

	private static Map<Long, String> test5(List<TestObj> testObjs) {
		Map<Long, String> map = new HashMap<Long, String>();
		for (TestObj testObj : testObjs) {
			map.put(testObj.getId(), testObj.getName());
		}
		return map;
	}

	private static void test6(Set<TestObj> testObjs1) {
		Map<Long, TestObj> map = new HashMap<Long, TestObj>();
		for (TestObj testObj : testObjs1) {
			map.put(testObj.getId(), testObj);
		}
	}

	private static void test7(Set<TestObj> testObjs1) {
		Map<Long, String> map = new HashMap<Long, String>();
		for (TestObj testObj : testObjs1) {
			map.put(testObj.getId(), testObj.getName());
		}
	}

	private static void test8(Map<Long, TestObj> map) {
		List<TestObj> list = new ArrayList<TestObj>();
		for (Long key : map.keySet()) {
			TestObj testObj1 = map.get(key);
			list.add(testObj1);
		}

	}

	private static void test9(Map<Long, String> longStringMap) {
		List<TestObj> list = new ArrayList<TestObj>();
		for (Long key : longStringMap.keySet()) {
			TestObj testObj1 = new TestObj();
			testObj1.setId(key);
			testObj1.setName(longStringMap.get(key));
			list.add(testObj1);
		}
	}
}
